import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    userPhone: string;
    userPwd: string;
    userCaptcha: string;
    userUid: string;
  }

  export interface LogoutParams {
    token: string;
    userId: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    token: string;
    userId: null;
    userName: string;
    userPhone: string;
    userAvatar: string;
    lastLoginIp: null | string;
    lastLoginTime: null | string;
    userSex: number;
    userEmail: string;
    userStatus: string;
    userDeptPost: [];
    superAdmin: any;
    createTime: string;
    homePath?: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }

  export interface Captcha {
    img: string;
    uuid: string;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('/auth/login', data);
}

/**
 * 登录
 */
export async function getCaptcha() {
  return requestClient.get<AuthApi.Captcha>('/captcha/image');
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi(
  logoutParams: AuthApi.LogoutParams,
): Promise<void> {
  return baseRequestClient.post('/auth/logout', logoutParams);
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}
